<?php

use app\assets\ApiAsset;
use app\assets\LayerAsset;
use app\assets\TableAsset;
use app\models\KeyMap;
use app\models\UserCardReserveRecharge;
use app\widgets\LinkPager;
use app\widgets\ManagerTableOp;
use yii\helpers\Html;
use yii\helpers\Url;
use dosamigos\datepicker\DatePicker;
/**
 * @var $this yii\web\View
 * @var $rechargeList app\models\UserCardRecharge[]
 * @var $pagination yii\data\Pagination
 */

ApiAsset::register($this);
LayerAsset::register($this);
TableAsset::register($this);

$this->title = '卡预充值列表';
$this->params['breadcrumbs'][] = '财务管理';
$this->params['breadcrumbs'][] = $this->title;
?>
<?php echo Html::beginForm('?', 'get', ['class'=>'form-inline']);?>
<div class="form-group">
    <label for="search_agentname" class="sr-only">代理</label>
    <?php echo Html::textInput('search_agentname', Yii::$app->request->get('search_agentname'), ['id' => 'search_agentname', 'class' => 'form-control', 'placeholder' => '代理', 'style' => 'width: 120px;']);?>
</div>
<div class="form-group">
    <label for="search_status" class="sr-only">来源</label>
    <?php echo Html::dropDownList('search_source', Yii::$app->request->get('search_source'), KeyMap::getValues('source'), ['id' => 'search_source', 'class' => 'form-control', 'prompt' => '来源']);?>
</div>
<div class="form-group">
    <label for="search_status" class="sr-only">执行状态</label>
    <?php echo Html::dropDownList('search_status',Yii::$app->request->get('search_status'),
        KeyMap::getValues('execute_status'),['id'=>'search_status','class' => 'form-control',
        'prompt' => '执行状态']);?>
</div>
<div class="form-group">
    <label for="search_iccid" class="sr-only">ICCID</label>
    <?php echo Html::textInput('search_iccid', Yii::$app->request->get('search_iccid'), ['id'=>'search_iccid', 'class'=>'form-control', 'placeholder'=>'ICCID', 'style' => 'min-width:180px;']);?>
</div>
<div class="form-group">
    <label for="time">激活时间</label>
    <?php echo DatePicker::widget([ 
        'name' => 'search_time', 
        'options' => ['placeholder' => '激活时间'], 
        'attribute' => 'time',
        'template' => '{addon}{input}',
        'value' => $current_time,
        'clientOptions' => [ 
            'autoclose' => true, 
            'format' => 'yyyy-mm-dd',
            'todayHighlight' => true, 
        ] 
    ]); ?>
</div>
<div class="form-group">
    <button class="btn btn-primary btn-sm">搜索</button>
</div>
<?php echo Html::endForm();?>
<table class="table table-striped table-bordered table-hover">
    <thead>
    <tr>
        <th class="center">
            <label class="pos-rel">
                <input type="checkbox" class="ace" />
                <span class="lbl">编号</span>
            </label>
        </th>
        <th class="center">代理商</th>
        <th class="center">ICCID/MSISDN/IMSI</th>
        <th class="center">套餐</th>
        <th class="center">充值信息</th>
        <th class="center">时长</th>
        <th class="center">金额</th>
        <th class="center">支付</th>
        <th class="center">订单号</th>
        <th class="center">是否执行充值</th>
        <th class="center">激活时间</th>
        <th class="center">来源</th>
        <th class="center">创建/执行时间</th>
        <th class="center">操作/预充值</th>
    </tr>
    </thead>

    <tbody>
    <?php foreach ($rechargeList as $recharge) {?>
        <tr>
            <td class="center"><label class="pos-rel"><input type="checkbox" class="ace" value="<?php echo $recharge->id;?>" /><span class="lbl"><?php echo $recharge->id;?></span></label></td>
            <td class="center"><?php echo empty($recharge->agent_id) ? '----' : Html::a(Html::encode($recharge->agent->username), ['/admin/user/agent-view', 'id' => $recharge->agent_id]);?></td>
            <td class="center"><?php echo empty($recharge->card->iccid) ? '----' : Html::encode($recharge->card->iccid);
                echo '<br />';
                echo empty($recharge->card->msisdn) ? '----' : Html::encode($recharge->card->msisdn);
                echo '<br />';
                echo empty($recharge->card->imsi) ? '----' : Html::encode($recharge->card->imsi);?></td>
            <td class="center"><?php echo Html::encode($recharge->package->name);?></td>
            <td class="center"><?php echo empty($recharge->card_recharge_id) ? '----' : Html::a(Html::encode($recharge->card_recharge_id), ['/admin/finance/user-card-recharge-list', 'search_id' => $recharge->card_recharge_id]);?></td>
            <td class="center"><?php echo $recharge->term_length, KeyMap::getValue('card_term_length_unit', $recharge->term_length_unit);?></td>
            <td class="center"><?php echo $recharge->money;?></td>
            <td class="center"><?php echo KeyMap::getValue('user_card_recharge_trade_type', $recharge->trade_type);?></td>
            <td class="center"><?php echo Html::encode($recharge->order_sn);?></td>
            <td class="center"><?php echo KeyMap::getValue('execute_status',$recharge->execute_status);?></td>
            <td class="center"><?php echo Yii::$app->formatter->asDate($recharge->active_time);?></td>
            <td class="center"><?php echo KeyMap::getValue('source',$recharge->source);?></td>
            <td class="center"><?php echo Yii::$app->formatter->asDatetime($recharge->create_time);
            echo '<br/>';
            echo empty($recharge->execute_time) ? '----' : Yii::$app->formatter->asDatetime($recharge->execute_time);?></td>
            <td class="center"><?php echo ManagerTableOp::widget(['items' => [
                    ['icon' => 'fa fa-info-circle','onclick' => 'view('.$recharge->id.')','btn_class' => 'btn btn-xs btn-default','tip' => '卡预充值详情'],
                    !($recharge->execute_status == 2) ?:['icon' => 'fa fa-check', 'onclick' => 'setExecute(' . $recharge->id . ')', 'btn_class' => 'btn btn-xs btn-warning', 'tip' => '执行预充值'],

                ]]);?>
            </td>
        </tr>
    <?php }?>
    </tbody>
</table>
<?php echo LinkPager::widget(['pagination' => $pagination]);?>
<script type="text/javascript">
    function setExecute(id) {
        layer.confirm('确定要执行预充值吗？', {icon: 3, title:'提示'}, function(index){
           $.getJSON('<?php echo Url::to(['/admin/finance/set-reserve-recharge-execute']);?>', {id:id}, function (json) {
                if (callback(json)) {
                    layer.msg('已执行预充值！', function () {window.location.reload();});
                }
            });
           layer.close(index);
        });
    }
    /**
    * 弹出卡充值详情    
     */
    function view(id){
        url = "<?php echo Url::to(['/admin/finance/reserve-recharge-view']);?>?id="+id;
        layer.open({
        type: 2, 
        closeBtn: 2,
        title:'卡预充值详情',
        offset: 'auto',
        area: ['750px', '800px'],
        shade: 0.5,
        shadeClose:true,
        content: url 
        }); 
    }
</script>

